/**
 * Created by DukeCrushIt on 2015/8/27.
 */
class MailItemRender extends eui.ItemRenderer{
    public constructor(){
        super();
        this.skinName = "pet.MailItemRenderSkin";
    }

    public onGet(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( this.data.hasAward){
            if( !this.data.awarded ){
                window["__mailAward"](this.data.id);
            }else{
                ShowHint("奖励已领取...");
            }
        }else{
            if(!this.data.read){
                window["__readMail"](this.data.id);
            }else{
                ShowHint("邮件已读...");
            }
        }
    }
    public onRemove(evt:egret.TouchEvent){
        window["__deleteMail"](JSON.stringify([this.data.id]));
    }
    public onRevenge(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( this.data.hasRevenge){
            if( !this.data.revenged ){
                var buff = BuffMgr.getInstance().getBuffById(BuffMgr.NO_WAR_BUFF);
                if(buff && buff.endTime>Model.SYSTIME){
                    RemindPanel.getInstance().showContent("您当前处于免战状态，一旦发起挑战将导致免战倒计时立即结束，是否继续挑战？",this, this.onClearBuff);
                    return;
                }
                window["__revenge"](this.data.id);
            }else{
                ShowHint("已复仇");
            }
        }
    }
    private onClearBuff(){
        window["__clearPeaceBuff"](1,this.data.id);
    }
    public img_remove:eui.Image;
    public img_get:eui.Image;
    public icon_head:eui.Image;
    public lab_content:eui.Label;
    public lab_name:eui.Label;
    public group_qty_0:eui.BitmapLabel;
    public ico_prize_0:eui.Image;
    public group_qty_1:eui.BitmapLabel;
    public ico_prize_1:eui.Image;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
        if(name == "img_get"){
            this.img_get = instance;
        }else if(name == "img_progressing"){
            this.icon_head = instance;
        }else if(name == "group_qty_0"){
            this.group_qty_0 = instance;
        }else if(name == "ico_prize_0"){
            this.ico_prize_0 = instance;
        }else if(name == "group_qty_1"){
            this.group_qty_1 = instance;
        }else if(name == "ico_prize_1"){
            this.ico_prize_1 = instance;
        }else if(name == "lab_name"){
            this.lab_name = instance;
        }else if(name == "img_remove"){
            this.img_remove = instance;
            this.img_remove.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRemove, this);
        }
    }

    public dataChanged(){
        super.dataChanged();
        this.reset();
        this.lab_content.text=this.data.content;
        this.lab_name.text = LimitStringLen(this.data.senderName,7);
        var prizeCnt:number = 0;
        if( Model.me.platform != PlatformEnum.QQBROWSER){
            this.icon_head.source = (this.data.senderFigureurl  && this.data.senderFigureurl.length != 0) ? this.data.senderFigureurl : RES.getRes("default_head_0") ;
        }else{
            this.icon_head.source = RES.getRes("default_head_"+(this.data.senderId%5));
        }

        if( this.data.hasAward){
            var hair:number = this.data.dogHair || this.data.catHair;
            if( hair != 0){
                this.ico_prize_0.source = this.data.catHair != 0 ? "img_money_cat" : "img_money_dog";
                // this.ico_prize_0.commitProperties();
                this.group_qty_0.text = GetNumString(hair,true,false);
                this.ico_prize_0.visible = this.group_qty_0.visible = true;
                prizeCnt++;
            }
            if( this.data.props.length != 0){
                var prop = this.data.props[0];
                if( prizeCnt == 0){
                    this.ico_prize_0.source = GameConst.Res_Root+"/resource/assets/items/"+prop[0]+".png?v="+GameConst.Version;
                    this.group_qty_0.text = prop[1]+"";
                    this.ico_prize_0.visible = this.group_qty_0.visible = true;
                }else{
                    this.ico_prize_1.source = GameConst.Res_Root+"/resource/assets/items/"+prop[0]+".png?v="+GameConst.Version;
                    this.group_qty_1.text = prop[1]+"";
                    this.ico_prize_1.visible = this.group_qty_1.visible = true;
                }
            }
        }

        if( this.data.hasRevenge){
            this.img_get.visible = true;
            if( !this.data.revenged){
                this.img_get.source = "img_revenge";
                this.img_get.touchEnabled = true;
                this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRevenge, this);
            }else{
                this.img_get.source = "img_revenge_1";
                this.img_get.touchEnabled = false;
            }
        }else if( this.data.hasAward){
            this.img_get.visible = true;
            if( !this.data.awarded){
                this.img_get.source = "img_get";
                this.img_get.touchEnabled = true;
                this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
            }else{
                this.img_get.source = "img_get_1";
                this.img_get.touchEnabled = false;
            }
        }else{
            this.img_get.visible = false;
        }
    }

    private reset(){
        this.img_get.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
        this.img_get.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRevenge, this);
        this.ico_prize_0.visible = false;
        this.group_qty_0.visible = false;
        this.ico_prize_1.visible = false;
        this.group_qty_1.visible = false;
    }
}